/*
ID: quxiao82
LANG: C++
PROG: agrinet
*/


#include <cstdio>
#include <cstring>
#include <cstdlib>

const int N = 101;
const int INF = 10000001;

int n;
int map[N][N];
int parent[N];
int inTree[N];
int dis[N];
int minCost;

void Input ()
{
       int i, j;
       scanf( "%d", &n);
       for ( i = 0; i < n; i ++)
             for ( j = 0; j < n; j ++)
                   scanf( "%d", &map[i][j]);
}

void Solve()
{
       int i, curNodeNum, minEdge, minIdx;
       for ( i = 0; i < n; i ++)
      {
             parent[ i] = -1;
             inTree[ i] = 0;
             dis[ i] = INF;
      }
       inTree[0] = 1;
       for ( i = 0; i < n; i ++)
      {
             if ( map[0][ i] )
            {
                   dis[ i] = map[0][ i];
                   parent[ i] = 0;
            }
      }
       minCost = 0;
       curNodeNum = 1;
       while ( curNodeNum < n )
      {
             minEdge = INF;
             for ( i = 0; i < n; i ++)
            {
                   if ( ! inTree[ i] && dis[ i] < minEdge )
                  {
                         minEdge = dis[ i];
                         minIdx = i;
                  }
            }
//          printf("edge: %d %d %d \n", parent[minIdx], minIdx, minEdge);
             inTree[ minIdx] = 1;
             minCost += minEdge;
             curNodeNum ++;
             for ( i = 0; i < n; i ++)
            {
                   if ( ! inTree[ i] && map[ minIdx][ i] && dis[ i] > map[ minIdx][ i] )
                  {
                         dis[ i] = map[ minIdx][ i];
                         parent[ i] = minIdx;
                  }
            }
      }

       printf( "%d\n", minCost);
}

int main ()
{
       freopen( "agrinet.in", "r" , stdin );
       freopen( "agrinet.out", "w" , stdout );

       Input();
       Solve();

       return 0;
}
